<?php
final class GuestBookModel {
	private $mysql;
	private $tableName = 'GuestBook';
	public $idRowName = 'GuestBook_ID';
	public $pubDateRowName = 'GuestBook_pubDate';
	public $descriptionRowName = 'GuestBook_description';
	public $CountRowName = 'GuestBook_Count';

	public function __construct ($mysql) {
		$this->mysql = $mysql;
	}

	public function GetGuestBookCount () {
		$ref = 0;
		$sqlresult = $this->mysql->query (sprintf ('SELECT COUNT(*) AS `%s` FROM `%s`;', $this->mysql->escape_string ($this->CountRowName), $this->mysql->escape_string ($this->tableName)));
		$len = $sqlresult->num_rows ();
		if ($len > 0) {
			$data = $sqlresult->fetch_array ();
			$ref = $data[$this->CountRowName];
		}
		$sqlresult->free_result ();
		return $ref;
	}

	public function GetGuestBookItems ($offset, $count) {
		$ref = array ();
		$sqlresult = $this->mysql->query (sprintf ('SELECT `%s`, `%s`, `%s` FROM `%s` ORDER BY `%s` DESC LIMIT %d, %d;', $this->mysql->escape_string ($this->idRowName), $this->mysql->escape_string ($this->pubDateRowName), $this->mysql->escape_string ($this->descriptionRowName), $this->mysql->escape_string ($this->tableName), $this->mysql->escape_string ($this->pubDateRowName), $this->mysql->escape_string ($offset), $this->mysql->escape_string ($count)));
		while ($data = $sqlresult->fetch_array ()) {
			$ref[] = $data;
		}
		$sqlresult->free_result ();
		return $ref;
	}

	public function DelGuestBookItem ($id) {
		$len = 0;
		$sqlresult = $this->mysql->query (sprintf ('DELETE FROM `%s` WHERE `%s`=\'%s\';', $this->mysql->escape_string ($this->tableName), $this->mysql->escape_string ($this->idRowName), $this->mysql->escape_string ($id)));
		$len = $sqlresult->affected_rows ();
		$sqlresult->free_result ();
		return $len;
	}

	public function AddGuestBookItem ($description) {
		$sqlresult = $this->mysql->query (sprintf ('INSERT INTO `%s` (`%s`, `%s`) VALUES (now(), \'%s\');', $this->mysql->escape_string ($this->tableName), $this->mysql->escape_string ($this->pubDateRowName), $this->mysql->escape_string ($this->descriptionRowName), $this->mysql->escape_string ($description)));
		$sqlresult->free_result ();
	}

}
?>